// page/coachs/course/index.js
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var week = date.getDay()
var startDay = new Date(year+','+ month+',' +'1').getDay()
var endDay = new Date(year, month, 0).getDate()
var selectDay = year + "-" + month + "-" + day
Page({
  data: {
    isHide: false,
    dateSelecd: "2017年12月8日",
    weeks: ['日', '一', '二', '三', '四', '五', '六'],
    year: year,
    month: month,
    day: day,
    today: day,
    startDay: startDay,
    activeIndex: "",
    showDate: false,
    status: [],
    course: [],
  },
  onLoad: function (options) {
    var that = this
    that.setData({
      isHide: true,
      course: [
        {
          period: "上午",
          periodIcon: "/images/icon/icon3.png",
          periodList: []
        },
        {
          period: "下午",
          periodIcon: "/images/icon/icon4.png",
          periodList: []
        },
        {
          period: "晚上",
          periodIcon: "/images/icon/icon5.png",
          periodList: []
        }
      ]
    })
    console.log(selectDay)
    that.request();
  },

  // 网络请求
  request: function () {
    var that = this
    wx.request({
      url: getApp().globalData.host + "&m=Coach&a=myCourse", //仅为示例，并非真实的接口地址
      data: {
        "uid": getApp().globalData.uid,
        "ymd": selectDay
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: "POST",
      success: function (res) {
        console.log(res.data)
        //请求成功
        if (res.data.status == 1) {
          that.setData({
            isHide: true,
            status: res.data.nd
          })
          for (var i = 0; i < that.data.status.length; i++) {
            console.log(that.data.status[i])
          }
          var arr = []
          for (let i = 0; i < endDay; i++) {
            arr.push({ day: i + 1, status: that.data.status[i] })
          }
          that.setData({
            days: arr
          })

          that.data.course[0].periodList = res.data.setTime0
          that.data.course[1].periodList = res.data.setTime1
          that.data.course[2].periodList = res.data.setTime2
          that.setData({
            course: that.data.course
          })
        } else {//请求失败
          wx.showToast({
            title: res.data.info,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "请求失败",
        })
      }
    })
  },

  bindShowDate: function () {
    this.setData({
      showDate: !this.data.showDate
    })
  },
  bindCloseDate: function () {
    this.setData({
      showDate: false
    })
  },
  //选择
  selected: function (e) {
    var that = this
    var idx = e.currentTarget.dataset.index
    that.setData({
      activeIndex: idx - startDay,
      day: that.data.days[idx - startDay].day,
      showDate: false
    })
    console.log(idx)
    selectDay = year + "-" + month + "-" + (idx + 1)
    console.log(selectDay)
    that.request();
  },
  notClick: function () {
    wx.showModal({
      title: '提示',
      content: '当前日期不可选',
      showCancel: false
    })
  },
  clickDes: function (e) {
    var that = this;
    var Index = e.currentTarget.dataset.index
    var subIndex = e.currentTarget.dataset.idx
    var timeStr = that.data.course[Index].periodList[subIndex].title
    var btnState = that.data.course[Index].periodList[subIndex].btnState
    console.log(btnState)
    if(btnState==0){
      that.openClose(timeStr)
    }else{
      wx.navigateTo({
        url: '/page/course/reservation?ymd=' + selectDay + "&timeStr=" + timeStr,
      })
    }
  },
  openClose: function (timeStr){
    wx.showLoading({
      title: '操作中',
      mask:true
    })
    var that = this
    wx.request({
      url: getApp().globalData.host + "&m=Coach&a=courseOnOff", //仅为示例，并非真实的接口地址
      data: {
        uid: getApp().globalData.uid,
        ymd: selectDay,
        timeStr: timeStr
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: "POST",
      success: function (res) {
        console.log(res.data)
        wx.hideLoading()
        //请求成功
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.info,
          })
          that.request();
        } else {//请求失败
          wx.showToast({
            title: res.data.info,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "请求失败",
        })
      }
    })
  }
})